﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExUtil;

namespace RuoVea.ExWeb
{
    /// <summary>
    /// 公共方法 可扩展
    /// </summary>
    public partial class BaseController : ControllerBase
    {
        #region 请求响应
        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        protected virtual IActionResult ToJsonResult(object data)
        {
            return Content(data.Json2Str());

        }
        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="info">消息</param>
        /// <returns></returns>
        protected virtual IActionResult Success(string info)
        {
            return Content(new RestfulResult { Code = CodeStatus.OK, Message = info }.Json2Str());
        }

        /// <summary>
        /// 返回成功数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        protected virtual ActionResult Success(object data)
        {
            return Content(new RestfulResult { Code = CodeStatus.OK, Message = CodeStatus.OK.GetDescription(), Data = data }.Json2Str());
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="info">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        protected virtual ActionResult Success(string info, object data)
        {
            return Content(new RestfulResult { Code = CodeStatus.OK, Message = info, Data = data }.Json2Str());
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="total">总行数</param>
        /// <returns></returns>
        protected virtual ActionResult PageDate(object data, int total)
        {
            return Content(new RestfulResult { Code = CodeStatus.OK, Data = new { data = data, total = total } }.Json2Str());
        }

        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="info">消息</param>
        /// <returns></returns>
        protected virtual ActionResult Fail(string info)
        {
            return Content(new RestfulResult { Code = CodeStatus.InternalServerError, Message = info }.Json2Str());
        }
        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="info">消息</param>
        /// <param name="data">消息</param>
        /// <returns></returns>
        protected virtual ActionResult Fail(string info, object data)
        {
            return Content(new RestfulResult { Code = CodeStatus.InternalServerError, Message = info, Data = data }.Json2Str());
        }
        #endregion
    }
}
